package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

//删除书籍
public class DelQperation implements IOperation{
    @Override
    public void word(BookList bookList){
        System.out.println("删除书籍");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除的书籍的名字:>");
        String name = scanner.nextLine();
        for (int i = 0; i < bookList.getUsedSize(); i++) {
            Book book = bookList.getBook(i);
            if(name.equals(book.getName())){
                //删除
                int pos = bookList.getUsedSize();
                for (int j = i+1; j < pos; j++) {
                    bookList.setBooks(j-1,bookList.getBook(j));
                    //Book book1 = bookList.getBook(j);
                    //bookList.setBooks(j-1,book1);
                }
                //将最后一个设置为空,防止内存泄漏
                bookList.setBooks(pos-1,null);
                //大小减一
                bookList.setUsedSize(pos-1);
                System.out.println("删除成功");
                return;
            }
        }
        System.out.println("无此书");
    }
}
